<template>
  <div class="pet-list-container">
    <div class="pet-header">
      <h2>我的宠物</h2>
      <button class="add-pet-btn" @click="showAdoptModal = true">
        <span class="icon">🐾</span>
        领养新宠物
      </button>
    </div>

    <!-- 宠物列表 -->
    <div class="pet-grid">
      <div 
        v-for="pet in pets" 
        :key="pet.id" 
        class="pet-card"
        @click="selectPet(pet)"
      >
        <div class="pet-avatar">
          <span class="pet-icon">{{ getPetIcon(pet.type) }}</span>
          <span class="mood-indicator" :title="pet.mood">{{ getMoodIcon(pet.mood) }}</span>
        </div>
        
        <div class="pet-info">
          <h3>{{ pet.name }}</h3>
          <div class="pet-stats">
            <div class="stat">
              <span class="icon">❤️</span>
              <div class="progress-bar">
                <div 
                  class="progress" 
                  :style="{ width: pet.health + '%' }"
                  :class="{ low: pet.health < 30 }"
                ></div>
              </div>
            </div>
            
            <div class="stat">
              <span class="icon">🍖</span>
              <div class="progress-bar">
                <div 
                  class="progress" 
                  :style="{ width: pet.hunger + '%' }"
                  :class="{ low: pet.hunger < 30 }"
                ></div>
              </div>
            </div>
            
            <div class="stat">
              <span class="icon">⭐</span>
              <div class="progress-bar">
                <div 
                  class="progress" 
                  :style="{ width: pet.happiness + '%' }"
                  :class="{ low: pet.happiness < 30 }"
                ></div>
              </div>
            </div>
          </div>
          
          <div class="pet-skills">
            <span 
              v-for="skill in pet.skills" 
              :key="skill.id" 
              class="skill-badge"
              :title="skill.description"
            >
              {{ skill.icon }}
            </span>
          </div>
        </div>
        
        <div class="pet-actions">
          <button @click.stop="feedPet(pet)" :disabled="!canFeed(pet)">
            <span class="icon">🍖</span>
          </button>
          <button @click.stop="playWithPet(pet)" :disabled="!canPlay(pet)">
            <span class="icon">🎾</span>
          </button>
          <button @click.stop="trainPet(pet)" :disabled="!canTrain(pet)">
            <span class="icon">📚</span>
          </button>
        </div>
      </div>
    </div>

    <!-- 领养模态框 -->
    <div v-if="showAdoptModal" class="modal-overlay" @click="showAdoptModal = false">
      <div class="modal-content" @click.stop>
        <h3>领养新宠物</h3>
        <div class="pet-types">
          <div 
            v-for="type in availablePetTypes" 
            :key="type.id"
            class="pet-type-card"
            :class="{ selected: selectedPetType === type.id }"
            @click="selectedPetType = type.id"
          >
            <span class="pet-icon">{{ type.icon }}</span>
            <h4>{{ type.name }}</h4>
            <p>{{ type.description }}</p>
            <div class="pet-traits">
              <span v-for="trait in type.traits" :key="trait" class="trait">
                {{ trait }}
              </span>
            </div>
          </div>
        </div>
        
        <div class="adopt-form" v-if="selectedPetType">
          <input 
            v-model="newPetName" 
            placeholder="给宠物起个名字..."
            maxlength="10"
          />
          <button 
            class="adopt-btn" 
            @click="adoptPet"
            :disabled="!newPetName"
          >
            确认领养
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, computed } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';

export default defineComponent({
  name: 'PetList',
  
  setup() {
    const store = useStore();
    const router = useRouter();
    const showAdoptModal = ref(false);
    const selectedPetType = ref('');
    const newPetName = ref('');
    
    const pets = computed(() => store.state.pet.pets);
    
    const availablePetTypes = [
      {
        id: 'dog',
        name: '小狗',
        icon: '🐕',
        description: '忠诚的伙伴，善于帮助收获作物',
        traits: ['忠诚', '活泼', '善解人意']
      },
      {
        id: 'cat',
        name: '猫咪',
        icon: '🐱',
        description: '机灵的伙伴，能够驱赶害虫',
        traits: ['优雅', '独立', '机敏']
      },
      {
        id: 'rabbit',
        name: '兔子',
        icon: '🐰',
        description: '可爱的伙伴，能加速作物生长',
        traits: ['温顺', '敏捷', '幸运']
      }
    ];
    
    const getPetIcon = (type: string) => {
      const petType = availablePetTypes.find(t => t.id === type);
      return petType ? petType.icon : '🐾';
    };
    
    const getMoodIcon = (mood: number) => {
      if (mood >= 80) return '😊';
      if (mood >= 60) return '🙂';
      if (mood >= 40) return '😐';
      if (mood >= 20) return '😟';
      return '😢';
    };
    
    const selectPet = (pet: any) => {
      router.push(`/pet/${pet.id}`);
    };
    
    const canFeed = (pet: any) => pet.hunger < 100;
    const canPlay = (pet: any) => pet.happiness < 100;
    const canTrain = (pet: any) => true; // 可以根据具体逻辑调整
    
    const feedPet = async (pet: any) => {
      await store.dispatch('pet/feedPet', pet.id);
    };
    
    const playWithPet = async (pet: any) => {
      await store.dispatch('pet/playWithPet', pet.id);
    };
    
    const trainPet = async (pet: any) => {
      await store.dispatch('pet/trainPet', pet.id);
    };
    
    const adoptPet = async () => {
      if (selectedPetType.value && newPetName.value) {
        await store.dispatch('pet/adoptPet', {
          type: selectedPetType.value,
          name: newPetName.value
        });
        showAdoptModal.value = false;
        selectedPetType.value = '';
        newPetName.value = '';
      }
    };
    
    return {
      pets,
      showAdoptModal,
      selectedPetType,
      newPetName,
      availablePetTypes,
      getPetIcon,
      getMoodIcon,
      selectPet,
      canFeed,
      canPlay,
      canTrain,
      feedPet,
      playWithPet,
      trainPet,
      adoptPet
    };
  }
});
</script>

<style scoped>
.pet-list-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.pet-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.add-pet-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: #4CAF50;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.add-pet-btn:hover {
  background: #45a049;
  transform: translateY(-2px);
}

.pet-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.pet-card {
  background: white;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  cursor: pointer;
  transition: all 0.2s ease;
}

.pet-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.pet-avatar {
  position: relative;
  text-align: center;
  margin-bottom: 12px;
}

.pet-icon {
  font-size: 48px;
}

.mood-indicator {
  position: absolute;
  bottom: -8px;
  right: -8px;
  font-size: 24px;
}

.pet-info h3 {
  margin: 0 0 12px;
  text-align: center;
}

.pet-stats {
  margin-bottom: 16px;
}

.stat {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.progress-bar {
  flex: 1;
  height: 8px;
  background: #eee;
  border-radius: 4px;
  overflow: hidden;
}

.progress {
  height: 100%;
  background: #4CAF50;
  transition: width 0.3s ease;
}

.progress.low {
  background: #f44336;
}

.pet-skills {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.skill-badge {
  font-size: 20px;
  padding: 4px;
  background: #f5f5f5;
  border-radius: 4px;
}

.pet-actions {
  display: flex;
  justify-content: center;
  gap: 12px;
}

.pet-actions button {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: #f5f5f5;
  cursor: pointer;
  transition: all 0.2s ease;
}

.pet-actions button:hover:not(:disabled) {
  background: #e0e0e0;
  transform: scale(1.1);
}

.pet-actions button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  padding: 24px;
  border-radius: 12px;
  max-width: 600px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
}

.pet-types {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin: 20px 0;
}

.pet-type-card {
  padding: 16px;
  border: 2px solid #eee;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.pet-type-card.selected {
  border-color: #4CAF50;
  background: #f1f8e9;
}

.pet-type-card:hover {
  border-color: #4CAF50;
}

.pet-traits {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.trait {
  padding: 4px 8px;
  background: #f5f5f5;
  border-radius: 12px;
  font-size: 12px;
}

.adopt-form {
  margin-top: 20px;
  display: flex;
  gap: 12px;
}

.adopt-form input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.adopt-btn {
  padding: 8px 24px;
  background: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.adopt-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

@media (max-width: 768px) {
  .pet-grid {
    grid-template-columns: 1fr;
  }
  
  .pet-types {
    grid-template-columns: 1fr;
  }
  
  .adopt-form {
    flex-direction: column;
  }
}
</style> 